function hideAll() {
    hideElements(['select2', 'label2', 'select3', 'label3', 'select4',
        'label4', 'select5', 'label5', 'notes', 'notesLabel', 'submitButton', 'fieldLength']);
}
function change1(value1) {
    var strURL="JavaScript/phpHandlers/submitNewTT_1.php?value1="+value1;
    var req = getXMLHTTP();
    if (req) {
        req.onreadystatechange = function() {
            if (req.readyState == 4) {
                // only if "OK"
                if (req.status == 200) {

                    if (value1 == "selectOne") {
                        hideElements(['select2', 'label2', 'select3', 'label3', 'select4', 
                            'label4', 'select5', 'label5', 'notes', 'notesLabel', 'submitButton', 'fieldLength']);
                    }else {

                        var values = req.responseText;
                        var arr = values.split(",");
                        var select = document.getElementById('select2');
                        select.options.length = 0;
                        if (arr.length > 1) {
                            select.options.length = arr.length - 1;
                        }
                        select.options[0] = new Option("--Select One--", "--Select One--", true, false);
                        for (var i = 0; i < arr.length - 1; i++) {
                            select.options[i+1] = new Option(arr[i], arr[i], false, false);
                        }
                        document.getElementById("label2").innerText=arr[arr.length-1];
                        hideElements(['select3', 'label3', 'select4',
                            'label4', 'select5', 'label5', 'notes', 'notesLabel', 'submitButton', 'fieldLength']);

                        document.getElementById('select2').style.visibility='visible';
			document.getElementById('tti_label2').style.height='auto';
                        document.getElementById('label2').style.visibility='visible';
                    }
                } else {
                    alert("submitNewTT_ajax.issueChange\nThere was a problem while using XMLHTTP:\n" + req.statusText);
                }
            }
        }
        req.open("GET", strURL, true);
        req.send(null);
    } else {
        alert("ajax failed");
    }
}

function change2(value2) {
    var select1 = document.getElementById('select1');
    if (select1.options[select1.selectedIndex].value == "storePrinter") {
        var select2 = document.getElementById('select2');
        if (select2.options[select2.selectedIndex].value == "Kyocera FS1128MFP") {
            hideElements(['select3', 'select4',
                'label4', 'select5', 'label5', 'notes', 'notesLabel', 'submitButton', 'fieldLength']);
            document.getElementById('label3').style.visibility='visible';
	    document.getElementById('tti_label3').style.height='auto';
            document.getElementById("label3").innerText="A trouble ticket cannot be submitted for the Kyocera FS1128MFP. You must call the number on the printer for services.";
            return;
        }
    }
    var strURL="JavaScript/phpHandlers/submitNewTT_2.php?value2="+value2;
    var req = getXMLHTTP();
    if (req) {
        req.onreadystatechange = function() {
            if (req.readyState == 4) {
                // only if "OK"
                if (req.status == 200) {

                    if (value2 == "selectOne") {
                        hideElements(['select3', 'label3', 'select4',
                            'label4', 'select5', 'label5', 'notes', 'notesLabel', 'submitButton', 'fieldLength']);
                    } else {
                        var values = req.responseText;
                        var arr = values.split(",");
                        var select = document.getElementById('select3');
                        select.options.length = 0;
                        if (arr.length > 1) {
                            select.options.length = arr.length - 1;
                        }
                        select.options[0] = new Option("--Select One--", "--Select One--", true, false);
                        for (var i = 0; i < arr.length - 1; i++) {
                            select.options[i+1] = new Option(arr[i], arr[i], false, false);
                        }
                        document.getElementById("label3").innerText=arr[arr.length-1]


                        document.getElementById('label3').style.visibility='visible';
                        document.getElementById('select3').style.visibility = 'visible';
			document.getElementById('tti_label3').style.height='auto';
                        hideElements(['select4',
                            'label4', 'select5', 'label5', 'notes', 'notesLabel', 'submitButton', 'fieldLength']);
                    }
                } else {
                    alert("submitNewTT_ajax.issueChange\nThere was a problem while using XMLHTTP:\n" + req.statusText);
                }
            }
        }
        req.open("GET", strURL, true);
        req.send(null);
    } else {
        alert("ajax failed");
    }
}
function change3(value3) {
    if (value3 == "selectOne") {
        hideElements(['select4', 'label4', 'select5', 'label5', 'notes', 'notesLabel', 'submitButton', 'fieldLength']);
        return;
    }
    var select1 = document.getElementById('select1');
    var value = select1.options[select1.selectedIndex].value;
    if (value == "storePrinter" || value == "Maintenance" || value == "Merchandising") {
        document.getElementById('notesLabel').innerText = 'Additional Notes';
        hideElements(['select5', 'label5']);
        document.getElementById('notesLabel').style.visibility='visible';
        document.getElementById('notes').style.visibility='visible';
        document.getElementById('submitButton').style.visibility='visible';
	document.getElementById('fieldLength').style.visibility='visible';
	document.getElementById('tti_notesLabel').style.height='auto';
        return;
    }
    var strURL="JavaScript/phpHandlers/submitNewTT_3.php?value3="+value3;
    var req = getXMLHTTP();
    if (req) {
        req.onreadystatechange = function() {
            if (req.readyState == 4) {
                // only if "OK"
                if (req.status == 200) {
                    var values = req.responseText;
                    var arr = values.split(",");
                    var select = document.getElementById('select4');
                    select.options.length = 0;
                    if (arr.length > 1) {
                        select.options.length = arr.length - 1;
                    }
                    select.options[0] = new Option("--Select One--", "--Select One--", true, false);
                    for (var i = 0; i < arr.length - 1; i++) {
                        select.options[i+1] = new Option(arr[i], arr[i], false, false);
                    }
                    document.getElementById("label4").innerText=arr[arr.length-1]


                    document.getElementById('label4').style.visibility='visible';
                    document.getElementById('select4').style.visibility = 'visible';
		    document.getElementById('tti_label4').style.height='auto';
                    hideElements(['select5', 'label5', 'notes', 'notesLabel', 'submitButton', 'fieldLength']);
                }else {
                    alert("There was a problem while using \n\
                            XMLHTTP in submitNewTT_ajax.change3:\n" + req.statusText);
                }

            } 
        }
        req.open("GET", strURL, true);
        req.send(null);
    }else {
        alert("ajax failed");
    }
} 

function change4(value4) {
    var select1 = document.getElementById('select3');
    var value = select1.options[select1.selectedIndex].value;
    if (value4 == "selectOne") {
        hideElements(['select5', 'label5', 'notes', 'notesLabel', 'submitButton', 'fieldLength']);
        return;
    } else if (value4 == "Other" || value != "Attached Device") {
        document.getElementById('notesLabel').innerText = 'Additional Notes';
        hideElements(['select5', 'label5']);
        document.getElementById('notesLabel').style.visibility='visible';
        document.getElementById('notes').style.visibility='visible';
        document.getElementById('submitButton').style.visibility='visible';
	document.getElementById('fieldLength').style.visibility='visible';
	document.getElementById('tti_notesLabel').style.height='auto';
        return;
    }
    var select = document.getElementById('select5');
    select.options.length = 0;
    select.options.length = 2;

    select.options[0] = new Option("Not Working", "Not Working", true, false);
    select.options[1] = new Option("Not Present", "Not Present", false, false);
    document.getElementById('label5').innerText = 'Status: ';
    document.getElementById('select5').style.visibility='visible';
    document.getElementById('label5').style.visibility='visible';
    document.getElementById('tti_label5').style.height='auto';
    document.getElementById('notesLabel').style.visibility='visible';
    document.getElementById('notes').style.visibility='visible';
    document.getElementById('submitButton').style.visibility='visible';
    document.getElementById('fieldLength').style.visibility='visible';
    document.getElementById('tti_notesLabel').style.height='auto';
//var strURL="JavaScript/phpHandlers/submitNewTT_4.php?value4="+value4;
// currently not being used.

}
function change5(value) {
    document.getElementById('6').style.visibility='visible';
    document.getElementById('6').innerHTML="Additional Notes<br/><textarea id='notes' row='4' columns='75'></textarea>";
    document.getElementById('submitDiv').style.visibility='visible';
//document.getElementById('submitDiv').innerHTML="<input type='submit' value='Submit' class='submit' onclick='gatherValues();'/>";
}

function gatherValues() {
    var req = getXMLHTTP();
    if (req) {
        req.onreadystatechange = function() {
            var box1 = 'empty';
            var box2 = 'empty';
            var box3 = 'empty';
            var box4 = 'empty';
            var box5 = 'empty';

            if (document.getElementById('box1')) {
                box1 = document.getElementById('box1').value;
            }
            if (document.getElementById('box2')) {
                box2 = document.getElementById('box2').value;
            }
            if (document.getElementById('box3')) {
                box3 = document.getElementById('box3').value;
            }
            if (document.getElementById('box4')) {
                box4 = document.getElementById('box4').value;
            }
            if (document.getElementById('box5')) {
                box5 = document.getElementById('box5').value;
            }
            var strURL="JavaScript/phpHandlers/insertTT.php?box1=" + box1 + "&box2=" + box2 + "&box3=" + box3 + "&box4=" + box4 +  "&box5=" + box5;
            req.open("GET", strURL, true);
            req.send(null);
        }
    } else {
        alert("ajax failed");
    }
}


function hideElements(arr) {

    for (i = 0; i < arr.length; i++) {
        document.getElementById(arr[i]).style.visibility='hidden';
	var object = document.getElementById('tti_' + arr[i]);
	if (object) {
		document.getElementById('tti_' + arr[i]).style.height='0px';
	}
    }
}

function getXMLHTTP() {
    var xmlhttp=false;
    try{
        xmlhttp=new XMLHttpRequest();
    }
    catch(e)	{
        try{
            xmlhttp= new ActiveXObject("Microsoft.XMLHTTP");
        }
        catch(e){
            try{
                xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
            }
            catch(e1){
                xmlhttp=false;
            }
        }
    }
    return xmlhttp;
}document.getElementById('tti_label2').style.height='auto';
